﻿//Write a program to convert binary numbers to their decimal representation.


using System;

public class BinaryToDecimal
{
    public static void Main()
    {
        Console.WriteLine("Enter binary number to convert to decimal");
        Console.WriteLine("Number must consist only the digits '1' and '0'\nN = ");        
        string str = Console.ReadLine();       
        byte[] array = new byte[str.Length];
        FillArray(str, array);
        Console.Write("The decimal number is {0}\n", ConvertToDecimal(array));
    }

    private static void FillArray(string str, byte[] array)
    {
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = byte.Parse(str[array.Length - i - 1].ToString());
        }
    }

    public static long ConvertToDecimal(byte[] array)
    {
        long decimalNumber = 0;
        for (int i = 0; i < array.Length; i++)
        {
            long multiply = 1;
            if (array[i] != 0)
            {
                for (int j = 0; j < i; j++)
                {
                    multiply *= 2;
                }
                
            }
            decimalNumber += array[i] * multiply;
        }

        return decimalNumber;
    }
}